package exercise;


import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登录管理servlet
 */
public class ManageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ManageServlet 被调用...");

        //判断该用户是否登陆过
        HttpSession session = req.getSession();
        Object username = session.getAttribute("username");
        if (null == username) {
            //说明没有登陆过
            //重定向到登录页面
            System.out.println("没有登录过，重定向到登录页面");
            resp.sendRedirect(req.getContextPath() + "/exercise/login.html");
        } else {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.print("<h1>用户管理界面</h1>" +
                    "<h1>欢迎你:管理员 " + username.toString() + "</h1>");
        }

    }
}
